Imports System.drawing

Namespace Overlay
    ''' <summary>
    ''' The LineOverlay object is used to chart line series. Series must be of type jChart.Overlay.Series.LineSeries
    ''' </summary>
    ''' <remarks></remarks>
    Public Class LineOverlay
        Inherits OverLayBase

        ''' <summary>
        ''' oChartDimensions is a jChart.ChartDimensions object that describes the interior of the chart. oXAxis and 
        ''' oYAxis are the X and Y axes that should be used for series belonging to the OverLay object.
        ''' </summary>
        ''' <param name="oChartDimensions"></param>
        ''' <param name="oXAxis"></param>
        ''' <param name="oYAxis"></param>
        ''' <remarks></remarks>
        Public Sub New( _
            ByVal oChartDimensions As ChartDimensions, _
            ByVal oXAxis As XAxis, _
            ByVal oYAxis As YAxis)

            MyBase.New(oChartDimensions, oXAxis, oYAxis)
        End Sub

        Protected Overrides Sub DrawAllSeries(ByRef g As Graphics)
            '---Count all series
            Dim iCountOfSeries_Line As Integer = moSeries.Count

            For Each oSeries As Series.LineSeries In moSeries
                SeriesToLine(g, oSeries)
            Next
        End Sub

        Private Sub SeriesToLine(ByRef g As Graphics, ByVal oSeries As Series.LineSeries)
            '---Define area values.
            Dim iXPointCount As Integer = moXAxis.XAxisValues.GetUpperBound(0)

            Dim oTopValues(iXPointCount) As Single

            Dim iPrevXPos As Integer = 0
            Dim iPrevYPos As Integer = 0

            Dim iCurXPos As Integer = 0
            Dim iCurYPos As Integer = 0

            For iAreaPoint As Integer = 0 To iXPointCount
                '---Now fill in current area
                oTopValues(iAreaPoint) = oSeries.Values(iAreaPoint)

                iPrevXPos = iCurXPos
                iPrevYPos = iCurYPos

                iCurXPos = moXAxis.CalculateXPos(iAreaPoint)

                If oSeries.Values(iAreaPoint) = oSeries.NullValue Then
                    '---Don't do anything - null point

                ElseIf iAreaPoint > 0 AndAlso oSeries.Values(iAreaPoint - 1) = oSeries.NullValue Then
                    '---Don't draw line, but do draw indicator
                    iCurYPos = moYAxis.CalculateYPos(oSeries.Values(iAreaPoint))

                Else
                    '---Draw line
                    iCurYPos = moYAxis.CalculateYPos(oSeries.Values(iAreaPoint))

                    Dim iOutlineAlpha As Integer
                    Dim iInteriorAlpha As Integer

                    If oSeries.AlphaValue < 105 Then
                        iOutlineAlpha = oSeries.AlphaValue + 150
                        iInteriorAlpha = oSeries.AlphaValue
                    Else
                        iOutlineAlpha = oSeries.AlphaValue
                        iInteriorAlpha = oSeries.AlphaValue - 150
                    End If

                    If iAreaPoint > 0 Then
                        Dim oPen1 As New Pen(TransparentColor(oSeries.Color, iOutlineAlpha), 3)
                        oPen1.DashStyle = oSeries.LineType

                        Dim oPen2 As New Pen(TransparentColor(oSeries.Color, iOutlineAlpha), 1)
                        oPen2.DashStyle = oSeries.LineType

                        g.DrawLine(oPen1, iPrevXPos, iPrevYPos, iCurXPos, iCurYPos)
                        g.DrawLine(oPen2, iPrevXPos, iPrevYPos, iCurXPos, iCurYPos)
                    End If
                End If
            Next

            If Not oSeries.Indicator Is Nothing Then
                For iAreaPoint As Integer = 0 To iXPointCount
                    '---Now fill in current area
                    oTopValues(iAreaPoint) = oSeries.Values(iAreaPoint)

                    iPrevXPos = iCurXPos
                    iPrevYPos = iCurYPos

                    iCurXPos = moXAxis.CalculateXPos(iAreaPoint)

                    If oSeries.Values(iAreaPoint) <> oSeries.NullValue Then
                        iCurYPos = moYAxis.CalculateYPos(oSeries.Values(iAreaPoint))

                        Dim oIndicatorFactory As New IndicatorFactory
                        oIndicatorFactory.Draw(oSeries.Indicator, g, iCurXPos, iCurYPos)
                    End If
                Next
            End If

            Dim oRegionList As ArrayList = Nothing
            oRegionList = CalculateLineRegions(g, oSeries, oTopValues, 8)
            For Each oRegion As RegionArea In oRegionList
                If oRegion.YValue <> oSeries.NullValue Then
                    MyBase.AddLineRegion(oRegion)
                End If
            Next
        End Sub
    End Class
End Namespace